package com.inexas.cu.client.register;

import com.google.gwt.validation.client.*;
import com.inexas.cu.client.mvp.*;

/**
 * The Presentation Model for signing in. SignInPm is subclass of
 * {@link PmObservable} so that we can observe changes to any of the properties of
 * the class.
 * 
 */
public class RegisterPm extends PresentationMangerImpl {

	private final Property<String> accountName = new PropertyImpl<String>("accountName");
	@Length(minimum = 3, maximum = 20)

	private final Property<String> password = new PropertyImpl<String>("password");

	private final Actuator signInActuator = new ActuatorImpl("signIn");

	/*
	 * Note about validation. The fields below are a hack to get validation
	 * working in the PM - but this is temporary. The library I'm using seems to
	 * be the implementation of choice (only?) of JSR303. It won't work properly
	 * with PM because we use Properties. There's not too much work to write the
	 * own validation library and gwt-validation has bugs and is not being
	 * updated very quickly so lets reinvent the wheel! In the mean time the
	 * hack.
	 * 
	 * Add a xxxValidate field for each of the properties. Have a listener
	 * update the validation rules. And don't forget to add a getter!
	 */
	private String accountNameValidate;
	private String passwordValidate;

	public RegisterPm() {
		super("Register");
	}

//	public RegisterPm(String accountNameString, String passwordString) {
//		// Set the values before we start observing
//		accountName.setValue(this, accountNameString);
//		password.setValue(this, passwordString);
//
//		// Event Aggregation - Fire an event for a change of the whole PM when
//		// any one of it's properties (but not activators) change
//
//		// Add validation and aggregation support...
//		accountName.add(new PmObserver() {
//			public void onChanged(Source source) {
//				accountNameValidate = accountName.getValue();
//				final List<PresentationManager.ValidationError> validationErrors = validate("accountNameValidate");
//				accountName.setValidationErrors(source, validationErrors);
//				fire(source);
//			}
//		});
//		this.password.add(new PmObserver() {
//			public void onChanged(Source source) {
//				passwordValidate = password.getValue();
//				final List<PresentationManager.ValidationError> validationErrors = validate("passwordValidate");
//				password.setValidationErrors(source, validationErrors);
//				fire(source);
//			}
//		});
//	}
//
//	private List<PresentationManager.ValidationError> validate(String propertyName) {
//		final List<PresentationManager.ValidationError> result;
//
//		final Set<InvalidConstraint<RegisterPm>> constraints = validator.validateProperty(this, propertyName);
//		if(constraints.isEmpty()) {
//			result = null;
//		} else {
//			result = new ArrayList<PresentationManager.ValidationError>();
//			for(final InvalidConstraint<? extends PresentationManager> constraint : constraints) {
//				final PresentationManager.ValidationError validationError = new PresentationManager.ValidationError(constraint);
//				result.add(validationError);
//			}
//		}
//		return result;
//	}
//
	public Property<String> getAccountName() {
		return accountName;
	}

	public Property<String> getPassword() {
		return password;
	}

	public Actuator getSignInActuator() {
		return signInActuator;
	}

	public String getAccountNameValidate() {
		return accountNameValidate;
	}

	public String getPasswordValidate() {
		return passwordValidate;
	}
}
